package weaver.interfaces.expense.action.gzc;

import weaver.conn.RecordSet;
import weaver.general.BaseBean;
import weaver.general.Util;
import weaver.interfaces.util.ExpenseCompare;
import weaver.interfaces.util.ExpenseCompareUtil;
import weaver.interfaces.util.ExpenseInfoUtil;
import weaver.interfaces.workflow.action.Action;
import weaver.soa.workflow.request.RequestInfo;
import weaver.workflow.request.RequestManager;

public class YSKFWZLYZDS_ExpenseAction extends BaseBean
  implements Action
{
  public String execute(RequestInfo requestInfo)
  {
    try
    {
      String workflowid = requestInfo.getWorkflowid();
      String requestid = requestInfo.getRequestid();
      writeLog("进入YSKFWZLYZDS_ExpenseAction-----------" + requestid);
      RecordSet rs = new RecordSet();
      rs.executeSql("select formid from workflow_base where id = " + workflowid);
      rs.next();
      String formid = rs.getString("formid");
      String formtable = "formtable_main_" + formid.replaceAll("-", "");
      ExpenseInfoUtil expenseInfoUtil = new ExpenseInfoUtil();
      String ccode = "";
      double amount = 0.0D;
      String cexpenseitemcode = "";
      String citemcode = "";
      String iyear = "";
      String iskt = "0";

      rs.execute("select id,b.ks,a.zcxm,a.je,b.zj,b.sqrq,b.kth2,b.sf from " + formtable + "_dt1 a , " + formtable + " b where a.mainid=b.id and  b.requestid=" + requestid);
      while (rs.next()) {
        ccode = expenseInfoUtil.getKsbm(Util.null2String(rs.getString("ks")));
        amount = Util.getDoubleValue(rs.getString("je"), 0.0D);
        cexpenseitemcode = Util.null2String(rs.getString("zcxm"));
        String kth = Util.null2String(rs.getString("kth2"));
        iyear = Util.null2String(rs.getString("sqrq")).substring(0, 4);
        String sfkt = Util.null2String(rs.getString("sf"));

        ExpenseCompareUtil expenseCompareUtil = new ExpenseCompare();
        if ("0".equals(sfkt)) {
          iskt = "1";
          if (!expenseCompareUtil.compareExpense2(workflowid, cexpenseitemcode, kth, amount, iyear)) {
            requestInfo.getRequestManager().setMessageid("预算更新失败");
            requestInfo.getRequestManager().setMessagecontent("课题号:" + kth + " ,支出项目:" + expenseInfoUtil.getCItemName(cexpenseitemcode) + "的预算不足");
            return "0";
          }
        }

        if (("1".equals(sfkt)) && 
          (!expenseCompareUtil.compareExpense4(workflowid, cexpenseitemcode, amount, iyear))) {
          requestInfo.getRequestManager().setMessageid("预算更新失败");
          requestInfo.getRequestManager().setMessagecontent("支出项目:" + expenseInfoUtil.getCItemName(cexpenseitemcode) + "的预算不足");
          return "0";
        }
      }

      rs.beforFirst();
      while (rs.next()) {
        ccode = expenseInfoUtil.getKsbm(Util.null2String(rs.getString("ks")));
        amount = Util.getDoubleValue(rs.getString("je"), 0.0D);
        cexpenseitemcode = Util.null2String(rs.getString("zcxm"));
        String kth = Util.null2String(rs.getString("kth2"));
        iyear = Util.null2String(rs.getString("sqrq")).substring(0, 4);

        if (!expenseInfoUtil.subtractExpense(workflowid, ccode, cexpenseitemcode, citemcode, kth, amount, iyear, iskt)) {
        	//requestInfo.getRequestManager().setMessageid("预算更新失败");
        	//requestInfo.getRequestManager().setMessagecontent("预算更新失败");
          return "0";
        }
      }
    } catch (Exception e) {
      requestInfo.getRequestManager().setMessageid("预算更新失败");
      requestInfo.getRequestManager().setMessagecontent(e.getMessage());
      return "0";
    }

    return "1";
  }
}

